<?php

class DBCLASS {

    public $DBASE = "";
    public $CONN = "";
    public $_COUNT = "0";

    function DBCLASS($SERVER = "", $CONNECTION_STRING = "") {
        $conn = sqlsrv_connect($SERVER, $CONNECTION_STRING);
        if (!$conn) {
            echo "Connection could not be established.";
            exit;
        }
        $this->CONN = $conn;
        return true;
    }

    function select($sql = "", $fetch = "SQLSRV_FETCH_ASSOC") {
        $this->_COUNT++;
        if (empty($sql)) {
            return false;
        }
        if (empty($this->CONN)) {
            return false;
        }
        $conn = $this->CONN;
        $stmt = sqlsrv_query($conn, $sql);

        if ($stmt === false) {
            return false;
        }

        $count = 0;
        $data = array();

        while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
            $data[$count] = $row;
            $count++;
        }
        sqlsrv_free_stmt($stmt);
        return $data;
    }

    function insert($sql = "", $isInsertId = true) {
        if (empty($sql)) {
            return false;
        }
        if (empty($this->CONN)) {
            return false;
        }

        $conn = $this->CONN;
        $stmt = sqlsrv_query($conn, $sql);

        // print_r($stmt);

        if ($stmt === false) {
            $this->error("<H2>Cannot insert</H2>\n");
            return false;
        }

        if ($isInsertId) {
            //Logic for retrieving last insert id
            $insert_query = "SELECT SCOPE_IDENTITY() as 'lastinserid'";
            $lastInsertIdArray = $this->select($insert_query);
            return $lastInsertIdArray['0']['lastinserid'];
        }
    }

    function query($sql = "") {
        if (empty($sql)) {
            return false;
        }
        if (empty($this->CONN)) {
            return false;
        }

        $conn = $this->CONN;
        $stmt = sqlsrv_query($conn, $sql);


        if ($stmt === false) {
            $this->error("<H2>Cannot insert</H2>\n");
            return false;
        }
    }

    /*
      function select_database($dbase) {
      $conn = $this->CONN;
      $this->DBASE = $dbase;

      if (!mysql_select_db($dbase, $conn)) {
      $this->error("Dbase Select failed");
      }
      }

      function close() {
      $conn = $this->CONN;
      $close = mysql_close($this->CONN);
      unset($this->CONN);
      if (!$close) {
      $this->error("Connection close failed");
      }
      return true;
      }

      function error($text) {
      return;
      #$conn = $this->CONN;
      #$no = mysql_errno($conn);
      #$msg = mysql_error($conn);
      exit;
      }

      function select($sql = "", $fetch = "mysql_fetch_assoc", $flag = 'No') {
      $this->_COUNT++;
      if (empty($sql)) {
      return false;
      }
      if (empty($this->CONN)) {
      return false;
      }
      $conn = $this->CONN;
      $results = mysql_query($sql, $conn);
      //write query in file
      #mysqli_next_result($conn);
      if ($flag == 'Yes')
      print_r($results);
      if ((!$results) or (empty($results))) {
      return false;
      }
      $count = 0;
      $data = array();
      while ($row = $fetch($results)) {
      $data[$count] = $row;
      $count++;
      }
      @mysql_free_result($results);
      return $data;
      }

      function selectfetch($sql = "", $fetch = "mysql_fetch_assoc", $assoc_fields = '') {
      $this->_COUNT++;
      if (empty($sql)) {
      return false;
      }
      if (empty($this->CONN)) {
      return false;
      }
      $conn = $this->CONN;

      $results = mysql_query($sql, $conn);
      #mysqli_next_result($conn);
      if ((!$results) or (empty($results))) {
      return false;
      }
      $count = 0;
      $data = array();
      if ($fetch == "mysql_fetch_assoc") {
      if ($assoc_fields == '') {
      while ($row = $fetch($results)) {
      $data[$count] = $row;
      $count++;
      }
      } else {
      while ($row = $fetch($results)) {
      $data[$row[$assoc_fields]][] = $row;
      }
      }
      } else {
      while ($row = $fetch($results)) {
      $data[$count] = $row;
      $count++;
      }
      }
      @mysql_free_result($results);
      return $data;
      }

      function execute($sql = "", $fetch = "mysql_fetch_assoc") {
      $this->_COUNT++;
      if (empty($this->CONN)) {
      return false;
      }
      $conn = $this->CONN;

      $results = @mysql_query($sql, $conn);
      #$res1 = mysqli_next_result($conn);
      #mysqli_next_result($conn);
      if ((!$results) or (empty($results))) {
      return false;
      }
      $count = 0;
      $data = array();
      while ($row = $fetch($results)) {
      $data[$count] = $row;
      $count++;
      }
      mysql_free_result($results);
      return $data;
      }

      function affected($sql = "") {
      $this->_COUNT++;
      if (empty($sql)) {
      return false;
      }
      if (!preg_match("/^select/", $sql)) {
      echo "wrongquery<br>$sql<p>";
      echo "<H2>Wrong function silly!</H2>\n";
      return false;
      }
      if (empty($this->CONN)) {
      return false;
      }
      $conn = $this->CONN;
      $results = @mysql_query($sql, $conn);
      if ((!$results) or (empty($results))) {
      return false;
      }
      $tot = 0;
      $tot = mysql_affected_rows();
      return $tot;
      }

      function insert($sql = "") {
      $this->_COUNT++;
      if (empty($sql)) {
      return false;
      }
      if (empty($this->CONN)) {
      return false;
      }
      $conn = $this->CONN;
      $results = mysql_query($sql, $conn);
      $this->writeQueryInFile('insert', $sql);
      #mysqli_next_result($conn);
      if (!$results) {
      $this->error("<H2>No results!</H2>\n");
      return false;
      }
      $id = mysql_insert_id($conn);
      return $id;
      }

      function sql_query($sql = "", $fetch = "mysql_fetch_assoc") {
      $this->_COUNT++;
      if (empty($sql)) {
      return false;
      }
      if (empty($this->CONN)) {
      return false;
      }
      $conn = $this->CONN;
      $results = mysql_query($sql, $conn) or die("query fail >> " . $sql . " <hr>");
      $this->writeQueryInFile('update', $sql);
      #mysqli_next_result($conn);
      if (!$results) {
      $message = "Query went bad!";
      $this->error($message);
      return false;
      }



      if (!preg_match("/^select/", $sql) && !preg_match("/^call/", $sql)) {
      return true;
      } else {
      $count = 0;
      $data = array();
      while ($row = @$fetch($results)) {
      $data[$count] = $row;
      $count++;
      }
      @mysql_free_result($results);
      return $data;
      }
      }

      // Call Below function to get the Results for a Query. It will return Two Dimesional Array.
      function select_assoc_array($sql = "", $key = '', $fetch = MYSQL_ASSOC) {
      $this->_COUNT++;
      if (empty($sql)) {
      return false;
      }
      if (!preg_match("/^select/", $sql) && !preg_match("/^call/", $sql)) {
      echo "wrongquery<br>$sql<p>";
      echo "<H2>Wrong function silly!</H2>\n";
      return false;
      }
      if (empty($this->CONN)) {
      return false;
      }
      $conn = $this->CONN;
      $results = @mysql_query($sql, $conn);
      if ((!$results) or (empty($results))) {
      return false;
      }
      $count = 0;
      $data = array();
      while ($row = mysql_fetch_array($results, $fetch)) {
      $data[$row[$key]][] = $row;
      $count++;
      }
      mysql_free_result($results);
      return $data;
      }

      //ends the class over here
      function writeQueryInFile($query_type, $query) {
      }
     */
}

?>
